/**
 * 
 */
package com.coder520.sync_learning.demo;

import java.util.concurrent.TimeUnit;

/**
 * @author zhaoyibo
 *  使用enumerate方法返回的线程数量会更加的准确，因为他只会返回活跃的线程
 */
public class ThreadGroupEnumerateThreads {
	public static void main(String[] args) throws InterruptedException {
		ThreadGroup myGroup=new ThreadGroup("MyGroup");
		
		
		//创建线程并且传入到threadGroup
		Thread thread=new Thread(myGroup,()->{
			while(true) {
				try {
					TimeUnit.SECONDS.sleep(1);
				}catch(Exception e) {
					e.printStackTrace();
				}
			}
		},"mythread");
		
		thread.start();

		TimeUnit.MILLISECONDS.sleep(2);
		
		ThreadGroup mainGroup=Thread.currentThread().getThreadGroup();
		
		Thread[] list=new Thread[mainGroup.activeCount()];
		
		int recueseSize=mainGroup.enumerate(list);
		
		System.out.println(recueseSize);
		
		recueseSize=mainGroup.enumerate(list, false);
		
		System.out.println(recueseSize);
				
		
	}
}
